/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sgs.dao;

import br.com.sgs.entity.Campus;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Eric
 */
public class DAOCampus extends AbstractDAO implements IDAOCampus{
    
    public DAOCampus(){
        super();
    }
    
    @Override
    public Campus findCampusById(int idcampus) {
        try {

            Query q = em.createQuery("SELECT c FROM Campus c WHERE c.id = :idcampus");
            q.setParameter("idcampus", idcampus);
            return (Campus) q.getSingleResult();

        } catch (Exception e) {
            System.err.println("Erro no findCampusById");
            return null;
        }
    }
   
    @Override
    public List<Campus> findCampusByAll(String descricao, Date dtcriacaoini, Date dtcriacaofin, List<String> lsstatus) {

        try {

            StringBuilder sql = new StringBuilder();

            if (!descricao.equals("")) {
                sql.append("  AND UPPER(c.descricao) LIKE '%").append(descricao.toUpperCase()).append("%'");
            }

            if (dtcriacaoini != null && dtcriacaofin != null) {
                sql.append(" AND c.dtcriacao BETWEEN :dtcriacaoini AND :dtcriacaofin ");
            }

            if (!lsstatus.isEmpty()) {
                String status = "";
                for (String st : lsstatus) {
                    status += st + ",";
                }
                status += lsstatus.get(0).toString();
                sql.append(" AND c.status  IN (").append(status).append(")");
            }

            Query q = em.createQuery("SELECT c FROM Campus c WHERE c.id > 0 " + sql.toString() + " ORDER BY c.descricao ASC");

            if (dtcriacaoini != null && dtcriacaofin != null) {
                q.setParameter("dtcriacaoini", dtcriacaoini);
                q.setParameter("dtcriacaofin", dtcriacaofin);
            }

            return q.getResultList();

        } catch (Exception e) {
            System.err.println("Erro na consulta findInstituicaByAll");
            e.printStackTrace();
            return null;
        }


    }
}
